<template>
  <div>
    <el-card>
      <!--卡片头部-->
      <div slot="header">
        <!--条件表单-->
        <el-form :inline="true" :model="selectForm" style="margin-left: -10px">
          <el-form-item label="ID" style="margin-right: 10px">
            <el-input placeholder="用户ID" v-model="selectForm.id"></el-input>
          </el-form-item>
          <el-form-item label="姓名" style="margin-right: 10px">
            <el-input placeholder="用户姓名" v-model="selectForm.name"></el-input>
          </el-form-item>
          <el-form-item label="会员等级" style="margin-right: 10px">
            <el-input placeholder="用户会员等级" v-model="selectForm.level"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="getAllData">查询</el-button>
            <el-button type="info" @click="clear">清空</el-button>
            <el-button type="danger" @click="deleteBatch" :disabled="ids.length <= 0">批量删除
            </el-button>
          </el-form-item>
        </el-form>
      </div>
      <!--卡片主体-->
      <div>
        <!--添加数据对话框表单-->
        <el-dialog :visible.sync="dialogVisible" ref="dialog" title="编辑用户信息" width="30%">
          <el-form :rules="rules" ref="updateForm" :model="user" label-width="70px" size="medium">
            <el-form-item label="ID">
              <el-input readonly v-model="user.id" style="width: 250px; margin-left: -30px;"></el-input>
            </el-form-item>
            <el-form-item label="姓名" prop="name">
              <el-input v-model="user.name" style="width: 250px; margin-left: -30px;"></el-input>
            </el-form-item>
            <el-form-item label="手机号" prop="number">
              <el-input v-model="user.number" style="width: 250px; margin-left: -30px;"></el-input>
            </el-form-item>
            <el-form-item label="积分">
              <el-input-number v-model="user.point" :min="0"
                               style="width: 250px; margin-left: -30px;"></el-input-number>
            </el-form-item>
            <el-form-item label="等级">
              <el-input-number v-model="user.level" :min="0" :max="3"
                               style="width: 250px; margin-left: -30px;"></el-input-number>
            </el-form-item>
            <el-form-item label="地址" prop="address">
              <el-input v-model="user.address" style="width: 250px; margin-left: -30px;"></el-input>
            </el-form-item>
            <el-form-item>
              <el-button style="margin-left: -40px" type="primary" @click="update">提交</el-button>
              <el-button @click="closeDialog">取消</el-button>
            </el-form-item>
          </el-form>
        </el-dialog>

        <!--数据表格-->
        <el-table :data="tableData" border @selection-change="selectIds">
          <el-table-column type="selection">
          </el-table-column>
          <el-table-column prop="id" label="ID" header-align="center" align="center">
          </el-table-column>
          <el-table-column prop="name" label="姓名" header-align="center" align="center">
          </el-table-column>
          <el-table-column prop="number" label="手机号" header-align="center" align="center">
          </el-table-column>
          <el-table-column prop="point" label="用户积分" header-align="center" align="center">
          </el-table-column>
          <el-table-column prop="level" label="会员等级" header-align="center" align="center">
          </el-table-column>
          <el-table-column prop="address" label="用户地址" header-align="center" align="center">
          </el-table-column>
          <el-table-column prop="operate" label="操作" header-align="center" align="center">
            <template slot-scope="scope">
              <el-button type="primary" size="mini" @click="findById(scope.row.id)">编辑</el-button>
              <el-button type="danger" size="mini" @click="deleteById(scope.row.id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>

        <br>

        <!--Pagination 分页-->
        <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="page"
                       :page-sizes="[5, 10, 15, 20]" :page-size="pageSize"
                       layout="total, sizes, prev, pager, next, jumper"
                       :total=parseInt(tableCount)>
        </el-pagination>
      </div>
    </el-card>
  </div>
</template>

<script>
import request from "@/utils/request";

export default {
  name: "UserView.vue",

  data() {
    return {
      //表单校验规则
      rules: {
        name: [
          {required: true, message: '请输入姓名', trigger: 'blur'},
          {min: 1, max: 20, message: '长度在 1 到 20 个字符', trigger: 'blur'}
        ],
        password: [
          {required: true, message: '请输入密码', trigger: 'blur'},
          {min: 1, max: 20, message: '长度在 1 到 20 个字符', trigger: 'blur'}
        ],
        number: [
          {required: true, message: '请输入手机号', trigger: 'blur'},
          {min: 11, max: 11, message: '长度需为 11 个字符', trigger: 'blur'}
        ],
        address: [
          {required: true, message: '请输入住址', trigger: 'blur'},
          {min: 1, max: 20, message: '长度在1到20个字符', trigger: 'blur'}
        ]
      },
      //表格数据
      tableData: [],
      //表格数据总条数
      tableCount: '',
      //条件表单
      selectForm: {
        id: '',
        name: '',
        level: ''
      },
      //用户数据
      user: {
        id: '',
        name: '',
        number: '',
        point: '',
        level: '',
        address: ''
      },
      //控制对话框是否可见
      dialogVisible: false,
      //页码
      page: 1,
      //每页记录数
      pageSize: 5,
      //批量删除id数组
      ids: []
    }
  },
  methods: {
    //打开对话框
    openDialog() {
      this.dialogVisible = true
    },

    //关闭对话框
    closeDialog() {
      this.dialogVisible = false
    },

    //清空条件表单
    clear() {
      this.selectForm = {}
      this.getAllData()
    },

    //批量选择id
    selectIds(val) {
      //提取每一行数据的id并封装到数组中
      this.ids = val.map(row => row.id)
    },

    //当前页码
    handleCurrentChange(val) {
      this.page = val;
      this.getAllData();
    },

    //每页记录数
    handleSizeChange(val) {
      this.pageSize = val;
      this.getAllData();
    },

    //查询数据
    getAllData() {
      //发送请求
      request.get('/user/admin/list', {
        params: {
          id: this.selectForm.id,
          name: this.selectForm.name,
          level: this.selectForm.level,
          page: this.page,
          pageSize: this.pageSize
        }
      }).then(res => {
        if (res.data.code === 200) {
          this.tableCount = res.data.data.total
          this.tableData = res.data.data.rows
        } else {
          this.$message.error(res.data.msg)
        }
      })
    },

    //根据id查询数据
    findById(id) {
      //打开数据对话框
      this.openDialog()
      //发送请求
      request.get(`/user/admin/${id}`).then(res => {
        if (res.data.code === 200) {
          this.user = res.data.data
        } else {
          this.$message.error(res.data.msg)
        }
      })
    },

    //编辑数据
    update() {
      //校验表单是否通过，通过则发送请求
      this.$refs['updateForm'].validate(valid => {
        if (valid) {
          //发送请求
          request.put('/user/admin', this.user).then(res => {
            if (res.data.code === 200) {
              this.closeDialog()
              this.getAllData()
              this.$message.success({message: '恭喜你，修改成功', type: "success"})
              this.user = {}
            } else {
              this.$message.error(res.data.msg)
            }
          })
        }
      })
    },

    //根据id删除数据
    deleteById(id) {
      //是否删除
      this.$confirm("此操作将删除该数据，是否继续？", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        //发送请求
        request.delete(`/user/admin/${id}`).then(res => {
          if (res.data.code === 200) {
            this.$message.success("恭喜你，删除成功")
            this.getAllData()
          } else {
            this.$message.error(res.data.msg)
          }
        })
      }).catch(() => {
        //用户点击取消按钮
        this.$message.info("已取消删除")
      })
    },

    //批量删除数据
    deleteBatch() {
      //是否删除
      this.$confirm("此操作将删除多行数据，是否继续？", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        //发送请求
        request.delete('/user/admin', {
          data: this.ids
        }).then(res => {
          if (res.data.code === 200) {
            this.$message.success("恭喜你，删除成功")
            this.getAllData()
          } else {
            this.$message.error(res.data.msg)
          }
        })
      }).catch(() => {
        //用户点击取消按钮
        this.$message.info("已取消删除")
      })
    },
  },
  created() {
    this.getAllData()
  },
}
</script>

<style>
@import "@/css/global.css";
</style>